package com.lf.web.service.system.module;

import com.lf.data.Cluster;
import com.lf.web.service.system.extension.Extension;
import com.lf.data.Global;
import com.lf.data.Page;
import com.lf.web.service.system.extension.ExtensionDAO;
import com.lf.data.search.Field;
import com.lf.data.search.Query;
import com.lf.web.service.ServiceSupport;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ModuleService extends ServiceSupport {

    private ModuleDAO moduledao;
    private ExtensionDAO extensiondao;

    public ModuleService() {
    }

    public void setModuledao(ModuleDAO moduledao) {
        this.moduledao = moduledao;
    }

    public void setExtensiondao(ExtensionDAO extensiondao) {
        this.extensiondao = extensiondao;
    }

    public String execute() {
        Page page = new Page();
        form.getBean(page);
        Field field = new Field();
        field.include(Module.TITLE, Module.EXT_KEY, Module.POSITION, Module.UPDATED);
        Cluster modules = moduledao.findPage(null, field, null, page);
        List<Map<String, Object>> list = (List<Map<String, Object>>) modules.get(Global.PAGE_LIST);
        field.clear();
        field.include(Extension.NAME);
        List<Object> keys = new ArrayList<>();
        for (Map<String, Object> mod : list) {
            Object obj = mod.get(Module.EXT_KEY);
            if (!keys.contains(obj)) {
                keys.add(obj);
            }
        }
        Query query = new Query();
        query.in(Extension.KEY, keys);
        List<Cluster> exts = extensiondao.find(query, field, null);
        context.put("modules", modules);
        context.put("exts", exts);
        return SUCCESS;
    }
}
